package com.ideabove.heart.game.character;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;

import com.ideabove.heart.data.Comment;
import com.ideabove.heart.game.StoryGirlState;
import com.ideabove.heart.game.framework.Sprite;
import com.ideabove.heart.manager.MoeManager;
import com.ideabove.heart.manager.ScheduleManager;
import com.ideabove.heart.util.StringUtil;


public class BasicGirl  extends Sprite{

	
	Rect m_BoundBox = new Rect();
	public float scaleX;
	public float scaleY;
	
	public Comment chInfo;
	public Bitmap bg;
	Bitmap dress;
	
	// 스프라이트 자체를 바꿔야 되는데 오늘은 그냥 이렇게 작업을 한다. 개선필요..
	ArrayList<Bitmap> face;
	long mFrameTimer;
	int mCurrentFrame;
	int mNoOfFrames;
	int mFPS = 1000 / 20;
	long mInterval;
	// face 에니메이션을 위해서..
	
	Bitmap hair;	
	
	boolean isTextShow;
	public Bitmap talkbox;
	StringUtil talk;
	int talkX;
	int talkY;
	Rect talkSRect;
	public Rect talkDesRect;
	 	
	Bitmap screen;
	Canvas screenCanvas;
	
	MediaPlayer  mPlayer;
	
	public BasicGirl(Comment chInfo) {
		this.chInfo = chInfo;
	}
	
	public void loadResource(){
		
		if(ScheduleManager.isTextOnOff((Activity)MoeManager.mCurContext)){
			isTextShow = true;
		}else{
			isTextShow = false;
		}
		
		Comment dressr = ScheduleManager.getDefaultDress((Activity)MoeManager.mCurContext);
//		String defaultDress = "dress_training.png";
//		String defaultHair = "hair_normal.png";
		
		String defaultDress = dressr.getDress();
		String defaultHair = dressr.getHair();
				
		String talkBox = "talk_box.png";
				
		MoeManager.addImg(talkBox);
		MoeManager.addImg(chInfo.getBg());
		MoeManager.addImgs(chInfo.getFace());
		
		if("df.png".equals(chInfo.getDress())){
			MoeManager.addImg(defaultDress);
		}else{
			MoeManager.addImg(chInfo.getDress());
			defaultDress = chInfo.getDress();
		}
		
		if("df.png".equals(chInfo.getHair())){
			MoeManager.addImg(defaultHair);
		}else{
			MoeManager.addImg(chInfo.getHair());
			defaultHair = chInfo.getHair();		
			ScheduleManager.setDefaultDress((Activity)MoeManager.mCurContext, defaultDress, defaultHair);
		}

		bg = MoeManager.getImg(chInfo.getBg());
		dress = MoeManager.getImg(defaultDress);
		
		face = MoeManager.getImgs(chInfo.getFace());
		mNoOfFrames = face.size(); // 다시 돌리는 구간.		
		
		hair = MoeManager.getImg(defaultHair);
		talkbox = MoeManager.getImg(talkBox);


		if(bg != null){
			scaleX = MoeManager.screenWidth / bg.getWidth();
	 		scaleY = MoeManager.screenHeight / bg.getHeight();
		
		    
		    // talkBox	    
		    float talkScaleX = MoeManager.screenWidth / MoeManager.DEFAULT_SC_WIDTH;
		    float talkScaleY = MoeManager.screenHeight / MoeManager.DEFAULT_SC_HEIGHT;
		    
			
			Paint pnt = new Paint();
		    pnt.setColor(Color.WHITE);
	//	    pnt.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);
	//	    pnt.setTypeface(Typeface.NORMAL);
		    float fontSize = 21  * talkScaleX;
		    
//		    if(fontSize > 32){
//		    	fontSize = 32;
//		    }
		    
		    pnt.setTextSize(fontSize);	   
//		    pnt.setTextScaleX(MoeManager.screenWidth / MoeManager.DEFAULT_SC_WIDTH);		
		    
		    talkX = 18;
		    talkY = (int)(MoeManager.screenHeight - talkbox.getHeight() * talkScaleY) ;
		    StringUtil st = new StringUtil();
			talk = st.setTextInfo(chInfo.getComment(), (int)(MoeManager.screenWidth - (40 * talkScaleX) ), 200, (int)(fontSize) + 2, pnt);
			
			talkSRect = new Rect(0,0,talkbox.getWidth(), talkbox.getHeight());
			
	//		if(st.textBoxHeight + 10 > talkbox.getHeight()){
	//			talkDesRect = new Rect(0,talkY - 80, 10 + (int)(talkbox.getWidth() * scaleX), talkY + (int)(st.textBoxHeight * scaleY) );
	//		}else{
	//			talkDesRect = new Rect(0,talkY - 80, 10 + (int)(talkbox.getWidth() * scaleX), talkY + talkbox.getHeight());
	//		}	
			
			talkDesRect = new Rect(0,talkY - 80, 10 + (int)(talkbox.getWidth() * talkScaleX), talkY + (int)(talkbox.getHeight() * talkScaleY));
		}
	}
	
	public void destory(){
		if(mPlayer != null){
			mPlayer.stop();
			mPlayer = null;
		}
		
	}
	

	public void stopVoice(){
		if(mPlayer != null){
			mPlayer.stop();
			mPlayer = null;
		}
	}
	
	public void init(){

		AssetFileDescriptor afd;
		try {
			afd = MoeManager.mCurContext.getAssets().openFd("voice/" + chInfo.getVoice());
			mPlayer = new MediaPlayer();
			mPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
			afd.close();			
			mPlayer.prepare();
			mPlayer.start();
			
			float volume = ScheduleManager.getCurrentVoiceVolume((Activity)MoeManager.mCurContext);
			mPlayer.setVolume(volume, volume);
			
			mPlayer.setOnErrorListener(new OnErrorListener() {
				
				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {
					// TODO Auto-generated method stub
					((Activity)MoeManager.mCurContext).finish();
					return false;
				}
			});
			
			mPlayer.setOnCompletionListener(new OnCompletionListener() {
				
				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub
					StoryGirlState state = (StoryGirlState)MoeManager.getCurrentState();
					state.moveNextState();
				}
			});

			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			((Activity)MoeManager.mCurContext).finish();
			e.printStackTrace();		
		}		
	}
	
	
	
	
	@Override
	public void draw(Canvas canvas) {
		// TODO Auto-generated method stub
		
//		canvas.drawBitmap(screen, 0, 0,null);
				
		canvas.save();
		canvas.scale(scaleX, scaleY);
		
		if(bg != null && !bg.isRecycled()){canvas.drawBitmap(bg, 0, 0, null);}
		if(dress != null && !dress.isRecycled()){canvas.drawBitmap(dress, 0, 0, null);}
		
		if(MoeManager.screenWidth <= MoeManager.DEFAULT_SC_WIDTH){
			if(face != null && face.size() > 0  && !face.get(mCurrentFrame).isRecycled()){
				canvas.drawBitmap((Bitmap)face.get(mCurrentFrame), 157, 119, null);
			}
			if(hair != null && !hair.isRecycled()){
				canvas.drawBitmap(hair, 84, 33, null);
			}
		}else{
			if(face != null && face.size() > 0 && !face.get(mCurrentFrame).isRecycled()){
				canvas.drawBitmap((Bitmap)face.get(mCurrentFrame), 234, 197, null);
			}
			if(hair != null && !hair.isRecycled()){
				canvas.drawBitmap(hair, 110, 50, null);
			}
		}		
		
		canvas.restore();
		
		if(isTextShow){ // 텍스트 모드(환경설정)
			if(talkbox != null && !talkbox.isRecycled()){canvas.drawBitmap(talkbox, talkSRect,talkDesRect, null);}		
			
			canvas.save();
			canvas.scale(1, 1);
			if(talk != null){talk.drawString(canvas, talkX, talkY);}
			canvas.restore();
		}
		
	}

	@Override
	public void Update(long GameTime){			
//		 super.Update(GameTime);				
		
		 if(GameTime > mFrameTimer + mFPS && mInterval <= 0) {
	            mFrameTimer = GameTime;
	            mCurrentFrame +=1;
	     
	            if(mCurrentFrame >= mNoOfFrames) {
	                mCurrentFrame = 0;
	                mInterval = GameTime;
	            }
	     }
		 
		 if( 3000 - (GameTime - mInterval) < 0){
			 mInterval = 0;
		 }
		 
//		if(bMove){
//			this.m_x += _dirX;
//			this.m_y += _dirY;
//		}
//		
//		m_BoundBox.set(m_x,m_y,m_x+42,m_y+44);	
	}

}
